package com.sikaryofficial.auth.controller;

import com.alibaba.nacos.shaded.com.google.common.collect.Maps;
import com.sikaryofficial.auth.form.LoginChannelBody;
import com.sikaryofficial.auth.form.RegisterChannelBody;
import com.sikaryofficial.auth.service.SysLoginService;
import com.sikaryofficial.common.core.constant.SecurityConstants;
import com.sikaryofficial.common.core.domain.R;
import com.sikaryofficial.common.core.exception.ServiceException;
import com.sikaryofficial.common.core.utils.ServletUtils;
import com.sikaryofficial.common.core.utils.StringUtils;
import com.sikaryofficial.system.api.RemoteUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.Objects;

/**
 * 渠道商客户登录注册接口
 *
 * @author qinjinyuan
 */
@RestController
@RequestMapping("/channel/customers")
public class ChannelLoginController {
    private static final Logger log = LoggerFactory.getLogger(ChannelLoginController.class);
    @Autowired
    private SysLoginService sysLoginService;
    @Autowired
    private RemoteUserService remoteUserService;
    /**
     * 渠道商-非密码登录
     * @param form
     * @return
     */
    @PostMapping("/login")
    public R<?> login(@Validated @RequestBody LoginChannelBody form) {
        if (StringUtils.isAnyBlank(form.getEmailAddress())) {
            throw new ServiceException("username is empty.");
        }
        // 前端不传登录用户名: username; 后端根据邮箱把用户名获取。
        String username = sysLoginService.getUserNameByEmail(form.getEmailAddress());
        if (StringUtils.isBlank(username)) {
            String brand = ServletUtils.getRequest().getHeader(SecurityConstants.BRAND_KEY);
            log.error("user is not exist.Email:{},brand:{}", form.getEmailAddress(), brand);
            throw new ServiceException("user is not exist.");
        }
        return R.ok(sysLoginService.loginOnly(username));
    }

    @PostMapping(value = "/register")
    public R<?> register(@Validated @RequestBody RegisterChannelBody registerBody) {
        return doRegister(registerBody);
    }

    /**
     * 无验证码接口
     * @param registerBody
     * @return
     */
    @PostMapping(value = "/noCaptcha/register")
    public R<?> noCaptchaRegister(@Validated @RequestBody RegisterChannelBody registerBody) {
        return doRegister(registerBody);
    }

    private R<Map<String, Object>> doRegister(RegisterChannelBody registerBody) {
        R<Boolean> checkEmailUnique = remoteUserService.checkEmailUnique(registerBody.getEmailAddress());
        if(Objects.nonNull(checkEmailUnique) && Boolean.TRUE.equals(checkEmailUnique.getData())){
            log.error("register email exist");
            throw new ServiceException("register email exist.");
        }
        // 用户注册
        Long userId = sysLoginService.channelRegister(registerBody);
        Map<String, Object> result = Maps.newHashMap();
        // 长整型字段,有溢出问题，调整为字符串返回
        result.put("userId", userId.toString());
        return R.ok(result);
    }
}
